#!/usr/bin/perl

#This program removes the Window's ^M line control 
#characters from all the files in a subdirectory.

#type ./strip.pl at the command line
#followed by the name of the directory with the files to be fixed
#./strip.pl directoryname

#no warranties are given or implied.
#use at your own risk


#get sub-directory in which files to be fixed are located

#to use this program make sure it is executable
#chmod 755 strip.pl


#to run this program 
#./strip.pl directoryname
#directory name being the name of the directory with your files that
#need to be fixed.



$directory = @ARGV[0];
chomp($directory);



#switch to sub-directory of files to scan and change
#read current working directory and store file names in @flist


opendir( INDIR, "./$directory") || die ("$0: Can not read directory");



while( $fname = readdir INDIR){
    next if $fname =~ /^\./;        #not hidden
    next if $fname =~ /^\.\.?$/;    #it's pwd or parent
    next if $fname =~ "strip.pl";   #not me
    push @flist, $fname;
}


closedir(INDIR);

system ("cd $directory");


foreach $fn (@flist) {

    open( IN, "$directory/$fn")  || die ("\n can not read $fn \n");
    open( OUT, ">temp") || die ("\n can not write $fn \n");
    $flag = 0;
    $lineNo = 0;

    while( <IN>){

	$lineNo++;
	$line = $_;
	
	$line =~ s/\r//g;
	print OUT "$line";
    }

    close(IN);
    close(OUT);
 
    rename (temp, $fn) || die "couldn't rename temp to $fn.\n";    
    
}



